# 代码结果：
    # NonTerminalExpression: Interpreting two
    # TerminalExpression: Interpreting two
    # TerminalExpression: Interpreting two
    # twotwo

from abc import ABC, abstractmethod

# 抽象表达式接口
class AbstractExpression(ABC):
    @abstractmethod
    def interpret(self, info: str) -> object:
        pass

# 终结表达式类
class TerminalExpression(AbstractExpression):
    def interpret(self, info: str) -> object:
        print(f"TerminalExpression: Interpreting {info}")
        return info

# 非终结表达式类
class NonTerminalExpression(AbstractExpression):
    def __init__(self, exp1: 'AbstractExpression', exp2: 'AbstractExpression'):
        self.exp1 = exp1
        self.exp2 = exp2

    def interpret(self, info: str) -> object:
        print(f"NonTerminalExpression: Interpreting {info}")
        return self.exp1.interpret(info) + self.exp2.interpret(info)

# 环境类
class Context:
    def __init__(self, exp: 'AbstractExpression'):
        self.exp = exp

    def parse(self, info: str) -> None:
        self.exp = self.exp.interpret(info)

    def get_value(self) -> object:
        return self.exp

# 客户端类
class Client:
    def main(self) -> None:
        terminal_expr = TerminalExpression()
        non_terminal_expr = NonTerminalExpression(terminal_expr, terminal_expr)
        context = Context(non_terminal_expr)
        context.parse("two")
        print(context.get_value())

# 客户端使用示例
if __name__ == "__main__":
    client = Client()
    client.main()